/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.View;

import com.lcfgly.Database.UserInfo;
import com.lcfgly.Exception.ActionException;
import com.lcfgly.Interface.UserSession;
import com.lcfgly.Service.MessageService;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 留言处理
 * @author lcfgly
 */
@Controller
@RequestMapping("/control")
public class Message {
    
    @Autowired
    MessageService messageService;
    
    @RequestMapping("/message")
    public String message(){
        return "controller/message";
    }
    
    @RequestMapping("/message/newmessage/{page}")
    @ResponseBody
    public Map newmessage(@PathVariable("page") int page,@UserSession UserInfo session){
        Map result=new HashMap();
        try{
           result.putAll(messageService.GetNotBackMessage(session.getUid(), page));
           result.put("error", false);
        }catch(ActionException e){
          result.put("error", true);
          result.put("message", e.getMessage());
        }
        return result;
    }
    
    @RequestMapping("/message/backmessagelist/{page}")
    @ResponseBody
    public Map backmessagelist(@PathVariable("page") int page,@UserSession UserInfo session){
        Map result=new HashMap();
        try{
           result.putAll(messageService.GetBackMessage(session.getUid(), page));
           result.put("error", false);
        }catch(ActionException e){
          result.put("error", true);
          result.put("message", e.getMessage());
        }
        return result;
    }
  
    @RequestMapping(value="/message/backmessage",method = RequestMethod.POST)
    @ResponseBody
    public Object backmessage(@RequestParam("id") int id,@RequestParam("back") String val,@UserSession UserInfo session){
        Map result=new HashMap();
        try{
            com.lcfgly.Database.Message msg=messageService.findbyId(id, session.getUid());
            msg.setBack(val);
            msg.setBacktime(new Date());
            messageService.save(msg);
            result.put("error", true);
        }catch(ActionException e){
            result.put("error", true);
            result.put("message", e.getMessage());
        }
        return result;
    }
}
